<?php

namespace App\Http\Controllers\V1\Api;

use App\Helper\AliyunSDK;
use App\Helper\UmengSDK;
use App\Models\V1\Users;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Ramsey\Uuid\Uuid;

class AuthController extends BaseController
{
    /**
     * 用户登录
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function login(Request $request)
    {
        //par 1账户 2手机
        $par = $request->input('par', '');
        $agree = $request->input('agree', '');
        $mobile = $request->input('mobile', "");
        $code = $request->input('code', "");
        $username = $request->input('username', "");
        if ($par == 1) {
            if (empty($agree)) {
                return ajax_output(300, "必须同意用户条款");
            }
            $credentials = $request->only(['username', 'password']);
            if (!$token = auth('box')->attempt($credentials)) {
                return ajax_output(300, "用户名或密码错误");
            }
            $user_model = new Users();
            $user = $user_model->where("username", $username)->where('state',1)->first();
            if (!$user) {
                return ajax_output(300, "没有这个用户或还没有审核通过");
            }
            return ajax_output(200, "成功", ['token' => $token]);
        } else {
            if (empty($agree)) {
                return ajax_output(300, "必须同意用户条款");
            }
            if (empty($mobile)) {
                return ajax_output(300, "请传手机号");
            }
            if (empty($code)) {
                return ajax_output(300, "请传验证码");
            }

            $key = 'sms' . $mobile;
            $cache_code = Cache::store('redis')->get($key);
            if ($cache_code != $code) {
                return ajax_output(300, "短信验证码错误");
            }

            $user_model = new Users();
            $user = $user_model->where("mobile", $mobile)->where('state',1)->first();
            if (!$user) {
                return ajax_output(300, "没有这个用户或还没有审核通过");
            }
            $token = auth('box')->login($user);
            return ajax_output(200, "成功", ['token' => $token]);
        }


    }




    /**
     * 忘记密码 步骤一
     * @param Request $request
     * @return void
     */
    public function forgot_step1(Request $request)
    {
        $username = $request->input('username', "");
        $user_model = new Users();
        $user = $user_model->where("username", $username)->first();
        if (!$user) {
            return ajax_output(300, "账号不存在");
        }
        if (empty($user->mobile)) {
            return ajax_output(300, "未绑定有效手机号码，联系客服找回");
        }
        return ajax_output(200, "成功", $user->mobile);
    }

    /**
     * 忘记密码 步骤二
     * @param Request $request
     * @return void
     */
    public function forgot_step2(Request $request)
    {
        $username = $request->input('username', "");
        $client_id = $request->input('client_id', "");
        $code = $request->input('code', "");
        if (empty($username) || empty($code)) {
            return ajax_output(300, "参数错误");
        }
        $user_model = new Users();
        $user = $user_model->where("username", $username)->first();
        if (empty($user->mobile)) {
            return ajax_output(300, "手机号码不全，请跟换其他方式找回");
        }
        $aliyun_sdk = new AliyunSDK();
        if (!$aliyun_sdk->verify_sms($user->mobile, $code, $client_id)) {
            return ajax_output(300, "短信验证码错误");
        }
        //重置用户的token值，用于三阶段校验
        $user->update([
            'token' => Uuid::uuid1(time())->toString()
        ]);
        return ajax_output(200, "成功", ['token' => $user->token]);
    }

    /**
     * 忘记密码 步骤三
     * @param Request $request
     * @return void
     */
    public function forgot_step3(Request $request)
    {
        $username = $request->input('username', "");
        $pasword = $request->input('password', "");
        $confirm_password = $request->input('confirm_password', "");
        $token = $request->input('token', "");
        if (empty($username) || empty($pasword) || empty($confirm_password) || empty($token)) {
            return ajax_output(300, "参数错误");
        }
        if ($pasword != $confirm_password) {
            return ajax_output(300, "两次密码不一致");
        }

        $user_model = new Users();
        $user = $user_model->where("username", $username)->first();
        if ($user->token != $token) {
            return ajax_output(300, "非法操作");
        }
        //更新密码
        $user->update([
            'password' => bcrypt($pasword)
        ]);
        return ajax_output(200, "成功");
    }

    /**
     * 退出登录
     * @return \Illuminate\Http\JsonResponse
     */
    public function logout()
    {
        if (auth('box')->user()) {
            auth('box')->logout();
        }
        return ajax_output(200, "成功");
    }


    /**
     * 找回密码
     * @param Request $request
     * @return void
     */
    public function retpas(Request $request)
    {

        $mobile = $request->input('mobile', "");
        $password = $request->input('password', "");
        $affirm_password = $request->input('affirm_password', "");
        $code = $request->input('code', "");

        if (empty($mobile) || empty($code) || empty($password) || empty($affirm_password)) {
            return ajax_output(300, "参数错误");
        }
        if ($password != $affirm_password) {
            return ajax_output(300, "两次密码不一样");
        }
        $key = 'sms' . $mobile;
        $cache_code = Cache::store('redis')->get($key);

        if ($cache_code != $code) {
            return ajax_output(300, "短信验证码错误");
        }

        $user_model = new Users();
        $user = $user_model->where("mobile", $mobile)->first();

        if (!$user) {
            return ajax_output(300, "账号不存在");
        }


        $res = $user_model->where('mobile',$mobile)->update([
            'password' => bcrypt($password),
        ]);

        if(!$res){
            return ajax_output(300, "找回失败");
        }else{
            return ajax_output(200, "成功", $user->mobile);
        }



    }


    /**
     * 注册用户
     * @param Request $request
     * @return void
     */
    public function register(Request $request)
    {

        $agree = $request->input('agree', '');
        $mobile = $request->input('mobile', "");
        $password = $request->input('password', "");
        $affirm_password = $request->input('affirm_password', "");
        $code = $request->input('code', "");

        if (empty($agree)) {
            return ajax_output(300, "必须同意用户条款");
        }
        if (empty($mobile) || empty($code) || empty($password) || empty($affirm_password)) {
            return ajax_output(300, "参数错误");
        }

        if ($password != $affirm_password) {
            return ajax_output(300, "两次密码不一样");
        }

        $key = 'sms' . $mobile;
        $cache_code = Cache::store('redis')->get($key);

        if ($cache_code != $code) {
            return ajax_output(300, "短信验证码错误");
        }
        $user_model = new Users();
        //查询用户名或手机号码是否存在
        $user = $user_model->where('mobile', $mobile)->first();
        if ($user) {
            return ajax_output(300, "手机号码已经存在");
        }
        $user_model->create([
            'password' => bcrypt($password),
            'mobile' => $mobile,
            'username' => $mobile,
        ]);
        return ajax_output(200, "注册成功");
    }


    /**
     * 发送短信
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function sendSms(Request $request)
    {
        $mobile = $request->input('mobile', '');
        if (empty($mobile)) {
            return ajax_output(300, "必须传手机号");
        }
        if (!isMobile($mobile)) {
            return ajax_output(300, "手机号格式不正确");
        }

        $code = rand(100000, 999999);
//        $code = 111111;
        $key = 'sms' . $mobile;
        Cache::store('redis')->put($key, $code, 300);
        Cache::store('redis')->get($key);
        return ajax_output(200, "发送成功", $code);


//       正试发送
        $code = rand(100000, 999999);
        $arr['mobile'] = $mobile;
        $arr['code'] = $code;
        $tem = alSendSms($arr);
        if ($tem) {
            $key = 'sms' . $mobile;
            Cache::store('redis')->put($key, $code, 300);
            return ajax_output(200, "发送成功");
        } else {
            return ajax_output(300, "发送失败");
        }


    }
}
